package com.shine.level.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * 罗马数字转整数
 * @author zhangshao
 * @date 2021/6/17 9:46 上午
 */
public class Roman2Int {
    private static Map<Character, Integer> random2IntMap = new HashMap<Character, Integer>(){{
        put('I',1);
        put('V',5);
        put('X',10);
        put('L',50);
        put('C',100);
        put('D',500);
        put('M',1000);
    }};

    public static int romanToInt(String romanStr){
        char[] charArray = romanStr.toCharArray();
        int pre = random2IntMap.get(charArray[0]);
        //结果返回值
        int res = 0;
        int cur = pre;
        for(int i =1;i<charArray.length;i++){
            cur = random2IntMap.get(charArray[i]);
            if(pre<cur){
                res -= pre;
            }else {
                res += pre;
            }
            pre = cur;
            //将前一个判断的值赋值给原数字
        }
        res+= cur;
        return res;
    }
    public static void main(String[] args) {
        String romanStr = "MCMXCIV";
        System.out.println(romanToInt(romanStr));

    }
}
